package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.AdminEntity;
import com.qfedu.entity.RoleEntity;
import com.qfedu.pojo.AdminPojo;
import com.qfedu.pojo.AuthPojo;
import com.qfedu.pojo.RolePojo;
import com.qfedu.service.AdminService;
import com.qfedu.service.RoleService;
import com.qfedu.util.Ajaxd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.filter.HttpPutFormContentFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/login.to")
    public String tologin() {
        System.out.println("jjj");
        return "login";
    }

    @RequestMapping("/login.do")
    @ResponseBody
    public Ajaxd dologin(AdminEntity adminEntity, HttpServletRequest request) {
        Ajaxd ajaxd = new Ajaxd();
        AdminPojo adminPojo = adminService.dologin(adminEntity);
        if (adminPojo == null || "".equals(adminPojo)) {
            ajaxd.setFlag(false);
        } else {
            //将当前用户所有权限放入session域当中
            List<RolePojo> rolePojoList = adminPojo.getRolePojoList();
            Set<AuthPojo> allauths=new HashSet<AuthPojo>();
            for (RolePojo rolePojo:rolePojoList){
                List<AuthPojo> authPojoList = rolePojo.getAuthPojoList();
                allauths.addAll(authPojoList);
            }
            //菜单权限
            Set<AuthPojo> firstauth=new HashSet<AuthPojo>();
            //菜单权限字符串
            StringBuffer authStr=new StringBuffer();
            for (AuthPojo authPojo:allauths){
                if (authPojo.getAutype().equals("菜单")){
                    firstauth.add(authPojo);
                }
                //将所有权限连接成字符串
                authStr.append("/").append(authPojo.getAupath());
            }
            HttpSession session = request.getSession();
            session.setAttribute("authStr",authStr.toString());
            session.setAttribute("fristauth",firstauth);
            session.setAttribute("authsSet",allauths);
            session.setAttribute("admin", adminPojo);
            ajaxd.setFlag(true);
        }
        return ajaxd;
    }

    @RequestMapping("/index.to")
    public String toindex() {
        return "index";
    }

    @RequestMapping("/welcome.to")
    public String towelcome() {
        return "admin/welcome";
    }

    @RequestMapping("/queryAdminlist.do")
    public String doadminlist(AdminEntity adminEntity, HttpServletRequest request) {
        PageHelper.startPage(adminEntity.getPageNum(), adminEntity.getPageSize());
        List<AdminPojo> list = adminService.queryAlladmin(adminEntity);
        PageInfo<AdminPojo> pageInfo = new PageInfo<AdminPojo>(list);
        request.setAttribute("account",adminEntity.getAacount());
        request.setAttribute("aname",adminEntity.getAname());
        request.setAttribute("pageinfo", pageInfo);
        return "admin/admin-list";
    }

    @RequestMapping("/deleteAdbyId.do")
    @ResponseBody
    public Ajaxd dodeleteByid(String aidstr) {
        Ajaxd ajaxd = new Ajaxd();
        Boolean flag = adminService.deleteAdById(aidstr);
        if (flag) {
            ajaxd.setFlag(true);
        } else {
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    @RequestMapping("/adminadd.to")
    public String toadminadd(HttpServletRequest request) {
        List<RolePojo> rolePojos = roleService.queryAllRole();
        request.setAttribute("rolelist", rolePojos);
        return "admin/admin-add";
    }

    @RequestMapping("/adminadd.do")
    public String doAdminAdd(AdminEntity adminEntity) {
//        Ajaxd ajaxd = new Ajaxd();
        Boolean aBoolean = adminService.addAdmin(adminEntity);
        if (aBoolean) {
            return "redirect: queryAdminlist.do";
        } else {
            return "error";
        }
    }

    //编辑用户
    @RequestMapping("/adminedit.to")
    public String toadminredit(AdminEntity adminEntity,HttpServletRequest request){
        AdminPojo adminPojo = adminService.dologin(adminEntity);
        List<RolePojo> rolePojos = roleService.queryAllRole();
        request.setAttribute("rolelist",rolePojos);
        request.setAttribute("adminpojo",adminPojo);
        return "admin/admin-edit";
    }

    @RequestMapping("/editAdmin.do")
    @ResponseBody
    public Ajaxd doeditAdmin(AdminEntity adminEntity){
        Ajaxd ajaxd = new Ajaxd();
        Boolean flag = adminService.untying(adminEntity);
        if (flag){
            ajaxd.setFlag(true);
        }else {
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    @RequestMapping("/updateAdminStatus.do")
    @ResponseBody
    public Ajaxd doupdateAdminStatus(AdminEntity adminEntity){
        if (adminEntity.getAstatus()==1){
            adminEntity.setAstatus(2);
        }else{
            adminEntity.setAstatus(1);
        }
        Ajaxd ajaxd = new Ajaxd();
        Boolean flag = adminService.updateAdminStatus(adminEntity);
        if (flag){
            ajaxd.setFlag(true);
        }else{
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    @RequestMapping("/logout.do")
    public String doLogout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "login";
    }
}
